import json

import pandas as pd
from pymongo import MongoClient

# 源client
client = MongoClient('mongodb://admin:yzy%40123456@10.11.2.56:27017/')
# 源数据库
origin_db = client["cm_data"]
origin_collection = origin_db['shezhen_recommend']

# 读取Excel文件
file_path = 'D:\\工作文档\\舌诊算法\\食材增加编码.xlsx'
df = pd.read_excel(file_path, header=0, engine='openpyxl')

# 显示前5行数据
# print(df)
json_data = df.to_json(orient='records', force_ascii=False)
print(json_data)
array_data = json.loads(json_data)
for item in array_data:
    # print(item["code"], item["name"])
    query = {"name": item["name"]}
    newvalues = {"$set": {"code": item["code"]}}
    x = origin_collection.update_many(query, newvalues)
    # results = origin_collection.find(query)
    # print(res["_id"])
    # for res in results:

# 根据字段查询数据
# query = {"name": "调理脾胃臂单举"}
# results = origin_collection.find(query)
#
# y_data1 = []
# # 打印查询结果
# for result in results:
#     print(result["name"], result["code"])
#     # expalins = result["explain"]
#     # for item in expalins:
#     #     y_data1.append(item["score"])
